// stores/useCollectionStore.ts
import { defineStore } from 'pinia';

interface CollectionItem {
  id: string;
  item: any;
}

export const useCollectionStore = defineStore('collection', {
  state: () => ({
    collections: {} as Record<string, CollectionItem>,
  }),
  actions: {
    initializeCollections() {
      // 从 localStorage 加载收藏数据
      const storedCollections = localStorage.getItem('collections');
      if (storedCollections) {
        this.collections = JSON.parse(storedCollections);
      } else {
        this.collections = {};
      }
    },
    toggleCollection(id: string, item: any) {
      if (this.collections[id]) {
        // 如果已经收藏，删除收藏
        delete this.collections[id];
      } else {
        // 如果没有收藏，添加收藏
        this.collections[id] = { id, item };
      }
      // 更新 localStorage
      localStorage.setItem('collections', JSON.stringify(this.collections));
    },
  },
});